﻿using HarmonyLib;
using PlayerRoles.PlayableScps.Scp096;
using PlayerRoles;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;

namespace CustomCharacter.Patches
{
    [HarmonyPatch(typeof(Scp096HitHandler), nameof(Scp096HitHandler.ProcessHits))]
    public static class ProcessHitsPatch
    {
        [HarmonyTranspiler]
        public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
        {
            List<CodeInstruction> inst = instructions.ToList();
            inst[71] = new CodeInstruction(OpCodes.Ldarg_0);
            inst.Insert(72, new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(Scp096HitHandler), nameof(Scp096HitHandler._scpRole))));
            inst.Insert(73, new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(PlayerRoleBase), nameof(PlayerRoleBase._lastOwner))));
            inst.RemoveAt(76);
            inst[76] = new CodeInstruction(OpCodes.Call,
                AccessTools.Method(typeof(HitboxIdentity), nameof(HitboxIdentity.IsEnemy), new System.Type[] { typeof(ReferenceHub), typeof(ReferenceHub) }));
            return inst;
        }
    }
}
